package com.yanqu.road.server.manger.activity;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.mall.MallActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.goods.MallWheelResult;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.mall.MallModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MallActivityMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(MallActivityMgr.class.getName());

    private static Map<Integer, MallActivityInfo> allActivityInfoMap;



    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    public static void reloadActivityData(Map<Integer, ActivityInfo> activityInfoMap, Map<Integer, Map<Integer, MallInfo>> activityMallInfoMap){
        logger.info("reload mall activity start");
        Map<Integer, MallActivityInfo> tempActivityInfoMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            if(ActivityMgr.activityInLoadTime(activityInfo)) {
                Map<Integer, MallInfo> mallInfoMap = activityMallInfoMap.get(activityInfo.getActivityId());
                if (null != mallInfoMap) {
                    MallActivityInfo mallActivityInfo = ActivityMgr.parseMallActivityInfo(activityInfo);
                    mallActivityInfo.setMallInfoMap(mallInfoMap);
                    tempActivityInfoMap.put(mallActivityInfo.getActivityId(), mallActivityInfo);
                }
            }
        }
        allActivityInfoMap = tempActivityInfoMap;
        playerReloadActivity();
        logger.info("reload mall activity end");
    }

    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        if(playerList.size() > 0) {
            List<MallActivityInfo> activityList = getInTimeActivityList();
            for(GamePlayer player : playerList){
                player.getModule(MallModule.class).sendMallActivity(activityList);
            }
        }
    }

    public static MallInfo getMallInfo(int mallId){
        List<MallActivityInfo> activityInfoList = getInTimeActivityList();
        for(MallActivityInfo activityInfo : activityInfoList){
            MallInfo mallInfo = activityInfo.getMallInfoMap().get(mallId);
            if(null != mallInfo){
                return mallInfo;
            }
        }
        return null;
    }

    public static List<MallActivityInfo> getInTimeActivityList(){
        List<MallActivityInfo> activityList = new ArrayList<>();
        for(MallActivityInfo activityInfo : allActivityInfoMap.values()){
            //象棋大逃杀要展示期也能买
            ActivityInfo acInfo = NormalActivityMgr.getActivityInfo(activityInfo.getActivityId());
            if(acInfo != null && ActivityMgr.isEndShowTimeCanBuyActivity(acInfo)){
                if(ActivityMgr.activityInShowTime(acInfo)) {
                    activityList.add(activityInfo);
                }
            }else{
                if(activityInTime(activityInfo)){
                    activityList.add(activityInfo);
                }
            }

        }
        return activityList;
    }

    private static boolean activityInTime(MallActivityInfo activityInfo) {
        if(null == activityInfo){
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        return activityInfo.getBeginTime() <= now && activityInfo.getEndTime() > now;
    }

    public static boolean activityInTime(int activityId) {
        MallActivityInfo activityInfo = allActivityInfoMap.get(activityId);
        return activityInTime(activityInfo);
    }

    public static MallActivityInfo getInTimeActivity(int activityId) {
        MallActivityInfo activityInfo = allActivityInfoMap.get(activityId);
        if (activityInTime(activityInfo)) {
            return activityInfo;
        }
        return null;
    }


    public static int getAllRound(int resetTime, Collection<MallInfo> mallInfoList){
        int max = 0;
        for (MallInfo mallInfo : mallInfoList) {
            if(mallInfo.getReSetTime() == resetTime){
                int round = Integer.parseInt(mallInfo.getExtendParam());
                if(round > max){
                    max = round;
                }
            }
        }
        return max;
    }

    public static MallWheelResult calWheel(ActivityInfo activityInfo, ServerInfo serverInfo){
        int openZeroTime = DateHelper.getZeroTime((int)serverInfo.getOpenTime());
        int beginZeroTime = DateHelper.getZeroTime((int)activityInfo.getBeginTime());
        //活动开始时的开服天数
        int openDay = (int)((Math.max(DateHelper.DAY_SECONDS, beginZeroTime - openZeroTime)) / DateHelper.DAY_SECONDS);

        int dayZeroTime = DateHelper.getTodayZeroTime();
        //活动开始到现在的天数
        int gapDay = (int)((dayZeroTime - beginZeroTime) / DateHelper.DAY_SECONDS);

        //开始档位
        int pos = 0;
        Map<String, ActivityConfig> configMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        if(configMap != null && configMap.containsKey("CRICKET_MALL_MARK_DYA")){
            List<String> posList = StringUtils.stringToStringList(configMap.get("CRICKET_MALL_MARK_DYA").getValue(), "\\|");
            for (int i = 0; i < posList.size(); i++) {
                String param = posList.get(i);
                List<Integer> list = StringUtils.stringToIntegerList(param, ";");
                if(openDay >= list.get(0)){
                    pos = i;
                }
            }
        }
        return new MallWheelResult(openZeroTime, beginZeroTime, gapDay, pos);
    }
}
